/**
 * @(#)StreamUtil.java 7/10/2009
 *
 * Copyright (c) 2005-2010 PsiqueWare S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information PsiqueWare S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n: 7/10/2009 - 11:46:36 AM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>7/10/2009 | <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.basico.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

/**
 * Utilidades para manejar streams.
 * <br/>Paquete Original: com.psiqueware.alheli.basico.util
 * <br/>Nombre  Original: StreamUtil
 * <br/>Oprima para ver : <a href="StreamUtil.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">AngelElectrico</a>
 * @version Revision: 1.1 7/10/2009 11:46:36 AM
 */
public final class StreamUtil
{


    /**
     * Constructor de StreamUtil.
     */
    public StreamUtil()
    {

    }

    /**
     * Lee el contenido el {@link InputStream} y lo convierte en una cadena.
     *
     * @param is a leer.
     *
     * @return la cadena leida.
     */
    public static String streamToString(InputStream is)
    {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                sb.append(line).append("\n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }


    /**
     * Copia el contenido de un {@link java.io.InputStream} a un {@link java.io.OutputStream}.
     *
     * @param origen  {@link java.io.InputStream} a leer.
     * @param destino {@link java.io.OutputStream} a escribir.
     */
    public static void copiarStream(final InputStream origen, final OutputStream destino)
    {
        InputStream from = null;
        OutputStream to = null;
        try
        {
            from = new BufferedInputStream(origen);
            to = new BufferedOutputStream(destino);
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1)
            {
                to.write(buffer, 0, bytesRead); // write
                buffer = new byte[4096];
            }
        }
        catch (final Exception e)
        {
            final String error = "<error><msg>No se pudo realizar la copia del archivo.</msg><parametros>" + origen + "</parametros></error>";
            System.err.println(error);
            System.err.println(e);
            e.printStackTrace();
        }
        finally
        {
            if (null != from)
                try
                {
                    from.close();
                }
                catch (IOException e)
                {
                    final String error = "<error><msg>No se pudo cerrar el buffer del archivo de origen.</msg><parametros>" + origen + "</parametros></error>";
                    System.err.println(error);
                    System.err.println(e);
                    e.printStackTrace();
                }
            if (null != to)
                try
                {
                    to.close();
                }
                catch (IOException e)
                {
                    final String error = "<error><msg>No se pudo cerrar el buffer del archivo de destino.</msg><parametros>" + origen + "</parametros></error>";
                    System.err.println(error);
                    System.err.println(e);
                    e.printStackTrace();
                }
        }
    }

}

